using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using System.Collections;
using OgilvyOne.Utility;

namespace OgilvyOne.SqlServerDAL
{
    public class ScoreRule : IScoreRule
    {
        // Methods
        public ScoreRuleInfo GetScoreRuleInfo(int id)
        {
            string cmdText = "select Score_Rule_ID,Score_Rule_Desc,Score_Rule_Type,Score_Rule_Value,Score_Rule_Flag from Score_Rule where Score_Rule_ID =" + id;
            ScoreRuleInfo info = null;
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    info = new ScoreRuleInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetInt32(3), reader.GetByte(4));
                }
                return info;
            }
        }

        public IList GetScoreRuleList(int flag)
        {
            string cmdText = "select Score_Rule_ID,Score_Rule_Desc,Score_Rule_Type,Score_Rule_Value,Score_Rule_Flag from Score_Rule where Score_Rule_Flag =" + flag;
            IList list = new ArrayList();
            ScoreRuleInfo info = null;
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    info = new ScoreRuleInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetInt32(3), reader.GetByte(4));
                    list.Add(info);
                }
            }
            return list;
        }

        public void Update(ScoreRuleInfo info)
        {
            string cmdText = string.Format("update Score_Rule set Score_Rule_Value ={0} where Score_Rule_ID ={1}", info.RuleValue, info.RuleID);
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}
